<?php

declare (strict_types = 1);

namespace App\Controller\Rbac;

use App\Controller\AbstractController;
use App\Service\Rbac\AuthRoleService;
use Hyperf\Di\Annotation\Inject;
use App\Validate\RoleValidation;

class RoleController extends AbstractController
{
    /**
     * @Inject
     * @var AuthRoleService
     */
    protected $roleService;

    public function roleList()
    {
        $page = (int)$this->request->input('page',1);
        $pageSize = (int)$this->request->input('pageSize',15);
        $result = $this->roleService->listRole($page, $pageSize);
        return $this->success('ok',$result['data'],$result['total']);
    }

    public function roleAdd()
    {
        $data = $this->request->inputs(['name','permission_id']);
        make(RoleValidation::class)->check($data, __FUNCTION__)->response();
        return $this->success('ok',$this->roleService->addRole($data));
    }

    public function roleEdit()
    {
        if ($this->request->isMethod('POST')){
            $data = $this->request->inputs(['id','name','permission_id']);
            make(RoleValidation::class)->check($data, __FUNCTION__)->response();
            $result = $this->roleService->editRolePost($data);
            return $this->success('修改成功',$result);
        }else{
            make(RoleValidation::class)->check($this->request->all(), 'roleShow')->response();
            return $this->success('获取资料',$this->roleService->editRoleGet((int)$this->request->input('id')));
        }
    }

    public function roleDel()
    {
        make(RoleValidation::class)->check($this->request->all(), 'roleShow')->response();
        return $this->success('删除成功',$this->roleService->delRole((int)$this->request->input('id')));
    }

    public function roleAll()
    {

        return $this->success('ok',$this->roleService->allRole(config('web.super')[0]));
    }
}